<?php

/**
 * Implements ResponseDataInterface for the specific handling of JSON type responses
 * Other response types(XML, AMF etc.) will have similar implementations 
 * @author Sukhbir Singh Grewal
 */
class JSONResponse implements ResponseDataInterface
{
	private $data;
	private $decoded_data;
	
	/** 
	 * Initialize the data object
	 * @param $data
	 */
	public function __construct($data)
	{
		if(empty($data))
			throw new ExceptionHandler('Invalid data argument');
		$this->data = $data;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see ResponseDataInterface::isValid()
	 */
	public function isValid()
	{
		$temp = json_decode($this->data);
		if(NULL === $temp)
			return false;
		else
		{
			$this->decoded_data = $temp;
			return true;
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * @see ResponseDataInterface::decode()
	 */
	public function decode()
	{
		if(!$this->isValid())
			throw new ExceptionHandler("Data could not be decoded");
		else
			return $this->decoded_data;
	}
}